/**
 * 
 */
package com.battcn.platform.service.dataAnalysis;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.battcn.platform.entity.crow.Statistics;
import com.battcn.platform.entity.crow.movie.Actor;
import com.battcn.platform.entity.crow.movie.ActorExample;
import com.battcn.platform.entity.crow.movie.Director;
import com.battcn.platform.entity.crow.movie.DirectorExample;
import com.battcn.platform.entity.crow.movie.Movie;
import com.battcn.platform.entity.crow.movie.MovieExample;
import com.battcn.platform.entity.crow.movie.MovieExample.Criteria;
import com.battcn.platform.mapper.crow.movie.ActorMapper;
import com.battcn.platform.mapper.crow.movie.DirectorMapper;
import com.battcn.platform.mapper.crow.movie.MovieMapper;

/**
 * @author yanglei 2018年1月31日 电影列表展示
 * 
 *         统计演员电影评分数据
 * 
 */
@Service
public class MovieService {
	@Autowired
	private ActorMapper actorMapper;
	@Autowired
	private MovieMapper movieMapper;
	@Autowired
	private DirectorMapper directorMapper;

	// 获取演员列表
	public List<Actor> getActorList(String name) {
		ActorExample example = new ActorExample();
		if (!StringUtils.isEmpty(name)) {
			example.createCriteria().andActornameLike("%" + name + "%");
		}
		List<Actor> list = actorMapper.selectByExample(example);
		for (Actor actor : list) {
			actor.setMovies(String.join(",", movieMapper.selectNameByMovies(actor.getMovieids().split(","))));
		}
		return list;
	}

	// 获取导演列表
	public List<Director> getDirectorList(String name) {
		DirectorExample example = new DirectorExample();
		if (!StringUtils.isEmpty(name)) {
			example.createCriteria().andDirectornameLike("%" + name + "%");
		}
		List<Director> list = directorMapper.selectByExample(example);
		for (Director director : list) {
			director.setMovies(String.join(",", movieMapper.selectNameByMovies(director.getMovieids().split(","))));
		}
		return list;
	}

	// 根据演员id获取电影信息
	public List<Movie> getMovieByActorId(int actorid) {
		Actor actor = selectByActorId(actorid);
		return getMovieByIds(actor.getMovieids());
	}

	// 根据演员id获取演员信息
	public Actor selectByActorId(int actorid) {
		Actor actor = actorMapper.selectByPrimaryKey(actorid);
		return actor;
	}

	public List<Movie> getMovieByIds(String movieIds) {
		if (!StringUtils.isEmpty(movieIds)) {
			MovieExample example = new MovieExample();
			List<Integer> movieids = new ArrayList<>();
			//用set特性去重
			Set<Integer> set = new  HashSet<Integer>(); 
			for (String str : movieIds.split(",")) {
				set.add(Integer.valueOf(str));
			}
			movieids.addAll(set);
			example.createCriteria().andIdIn(movieids);
			return movieMapper.selectByExample(example);
		}
		return null;

	}

	public List<Movie> getMovieByName(String name) {
		MovieExample example = new MovieExample();
		example.createCriteria().andTitleLike("%" + name + "%");
		return movieMapper.selectByExampleWithBLOBs(example);
	}

	// 根据导演id获取电影信息
	public List<Movie> getMovieByDirectorId(int directorId) {
		Director director = selectByDirectorId(directorId);
		return getMovieByIds(director.getMovieids());
	}

	// 根据演员id获取演员信息
	public Director selectByDirectorId(int directorId) {
		Director director = directorMapper.selectByPrimaryKey(directorId);
		return director;
	}

	// 电影年份统计
	public List<JSONObject> selectMovieAnnee() {
		return movieMapper.selectMovieAnnee();
	}

	
	/*
	 * 统计电影评分 评分大于7为好评
	 */
	public Statistics getMovieStatics(String movieIds) {
		Statistics statistics = new Statistics();
		List<Movie> movielist = getMovieByIds(movieIds);
		List<Movie> niceMovieList = new ArrayList<>();
		List<Movie> middleMovieList = new ArrayList<>();
		List<Movie> badMovieList = new ArrayList<>();
		List<Movie> rubbishList = new ArrayList<>();
		if (!movielist.isEmpty()) {
			Double sum = 0.0;
			for (Movie movie : movielist) {
				// 好评
				if (movie.getGrade() >= 7.0) {
					niceMovieList.add(movie);
				}
				// 还行
				if (movie.getGrade() > 5.0 && movie.getGrade() < 7.0) {
					middleMovieList.add(movie);
				}
				// 差评
				if (movie.getGrade() > 3.0 && movie.getGrade() <= 5.0) {
					badMovieList.add(movie);
				}
				// 垃圾
				if (movie.getGrade() <= 3.0) {
					rubbishList.add(movie);
				}
				sum += movie.getGrade();
			}
			// 平均值
			statistics.setAverage(sum / movielist.size());
			statistics.setNiceMovieList(niceMovieList);
			statistics.setBadMovieList(badMovieList);
			statistics.setMiddleMovieList(middleMovieList);
			statistics.setRubbishList(rubbishList);
			return statistics;
		}
		return null;
	}

	// 根据电影id查询演员数据
	public List<Actor> selectActorById(int movieid) {
		return movieMapper.selectActorById(movieid);
	}

	// 根据电影id查询导演
	public List<Director> selectDirectorById(int movieid) {
		return movieMapper.selectDirectorById(movieid);
	}

	// 根据id查询电影详情
	public Movie selectMovieById(int movieid) {
		return movieMapper.selectByPrimaryKey(movieid);
	}
}
